package org.fage.semples.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.fage.semples.model.ExamSubject;
import org.fage.semples.model.PagerModel;
import org.fage.semples.service.ExamSubjectHiberanteService;
import org.fage.semples.service.ExamUserHiberanteService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;


@Controller
public class SubController {

	@Autowired
	private ExamSubjectHiberanteService examSubjectHiberanteService;
	
	@Autowired
	private ExamUserHiberanteService examUserHiberanteService;
	
	@Autowired
	private LocaleResolver localeResolver;
	
//	@Autowired
//	private MessageSource messageSource;
//	
	//查询
	@RequestMapping("/subs")
	public String users(Model model){
		
		PagerModel<ExamSubject> page = examSubjectHiberanteService.findAllByPage();
		model.addAttribute("url", "subs");
		model.addAttribute("page_jsp", "1");
		model.addAttribute("page", page);
		
//		List<ExamSubject> list = examSubjectHiberanteService.findAllExamSubject();
//		model.addAttribute("edit",messageSource.getMessage("page.index.edit",null,LocaleContextHolder.getLocale()));
//		model.addAttribute("userList", list);
		return "subject/subUserList";
	}
	
	//新增
	@RequestMapping(value = "/subAdd", method = RequestMethod.GET)
	public String toAdd(Model model){
		model.addAttribute("examSubject", new ExamSubject());
		return "subject/subAdd";
	}
	
	@RequestMapping(value = "/subAdd", method = RequestMethod.POST)
	public String add(RedirectAttributes redirectAttributes,@Validated ExamSubject examSubject, BindingResult result){
		if(result.hasErrors()){
			return "subject/subAdd";
		}
		try{
			examSubjectHiberanteService.createExamSubject(examSubject);
			redirectAttributes.addFlashAttribute("message", "添加成功");
		}catch(Exception e){
			e.printStackTrace();
			return "error";
		}
		return "redirect:/subs";
	}
	
	//修改
	@RequestMapping(value = "/subEdit/{subId}", method = RequestMethod.GET)
	public String toEdit(Model model,@PathVariable String subId) throws Exception{
		ExamSubject examSubject = examSubjectHiberanteService.getExamSubjectById(subId);
		model.addAttribute("examSubject", examSubject);
		return "subject/subEdit";
	}
	
	@RequestMapping(value = "/subEdit/{subId}", method = RequestMethod.POST)
	public String edit(RedirectAttributes redirectAttributes,ExamSubject examSubject,@PathVariable String subId){
		try{
			
			examSubjectHiberanteService.editExamSubject(examSubject);
			redirectAttributes.addFlashAttribute("message", "修改成功");
		}catch(Exception e){
			e.printStackTrace();
			return "error";
		}
		return "redirect:/subs";
	}
	
	//删除
	@RequestMapping(value = "/subDelete/{subId}", method = RequestMethod.GET)
	public String delete(RedirectAttributes redirectAttributes, @PathVariable String subId){
		try {
			examSubjectHiberanteService.deleteExamSubject(subId);
			redirectAttributes.addFlashAttribute("message", "删除成功");
		} catch (Exception e) {
			redirectAttributes.addFlashAttribute("message", "删除失败");
			e.printStackTrace();
		}
		return "redirect:/subs";
	}
	
	//语言切换
	@RequestMapping(value = "/subChangeLocale")
	public String changeLocale(HttpServletRequest request,HttpServletResponse response, String localeType){
		if("en_US".equals(localeType)){
			localeResolver.setLocale(request,response,Locale.US);
		}else if("zh_CN".equals(localeType)){
			localeResolver.setLocale(request,response,Locale.CHINA);
		}
		return "redirect:subs";
	}
	
	//改Status
	@ResponseBody
	@RequestMapping("/subEditStatus")
	public void subEditStatus(String subId) throws Exception{
		ExamSubject examSubject = examSubjectHiberanteService.getExamSubjectById(subId);
		if(examSubject.getStatus().equals("0")){
			examSubject.setStatus("1");
			examSubjectHiberanteService.editExamSubject(examSubject);
			
		}else{
			examSubject.setStatus("0");
			examSubjectHiberanteService.editExamSubject(examSubject);
		}
	}
	
	//获取Status
	@ResponseBody
	@RequestMapping(value = "/getAllStudent")
	public Map<String, Object> getAllStudent(String subId){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("allUser", examUserHiberanteService.findAllStudent25());
		resultMap.put("selectedUser", examSubjectHiberanteService.getAllUserBySubjcet(subId));
		resultMap.put("score", examSubjectHiberanteService.getScoreBySubId(subId));
		return resultMap;
	}
	
	//修改表exam_subject_user
	@ResponseBody
	@RequestMapping("/saveSubjectStudents")
	public Map<String, Object> saveSubjectStudents(@RequestParam(name = "subjectStudents[]")String[] subjectStudents,Integer subjectId){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("success", false);
		List<String> list = new ArrayList<String>();
		for(String student : subjectStudents){
			list.add(student);
		}
		try{
			examSubjectHiberanteService.saveSubjectStudent(list,subjectId);
			resultMap.put("success", true);
		}catch(Exception e){
			e.printStackTrace();
		}
		return resultMap;
	}
	
}
